package jst.householdmanager.activities;

import java.util.ArrayList;
import java.util.List;

import jst.householdmanager.R;
import jst.householdmanager.database.ShopsDataSource;
import jst.householdmanager.database.table.Shops;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ShopListAct extends Activity {

	private static final int EDIT_SHOP_ACT_EDIT = 1000;

	private List<Integer> shopsId = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_shoplist);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		// Set list actions
		ListView shoplist_list = (ListView) findViewById(R.id.shoplist_list);
		shoplist_list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parentView, View selectedItemView, int position, long id) {
				Intent intent = new Intent(ShopListAct.this, EditShopAct.class);
				intent.putExtra("ShopId", ShopListAct.this.shopsId.get(position));
				startActivityForResult(intent, EDIT_SHOP_ACT_EDIT);
			}
		});

		shoplist_list.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parentView, View selectedItemView, int position, long id) {
				AlertDialog.Builder builder = new AlertDialog.Builder(ShopListAct.this);
				final int shopId = ShopListAct.this.shopsId.get(position);
				builder.setTitle(getText(R.string.selection));
				builder.setItems(new CharSequence[] { getText(R.string.edit), getText(R.string.delete) }, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						switch (item) {
						case 0:
							Intent intent = new Intent(ShopListAct.this, EditShopAct.class);
							intent.putExtra("ShopId", shopId);
							startActivityForResult(intent, EDIT_SHOP_ACT_EDIT);
							break;
						case 1:
							ShopListAct.this.deleteShop(shopId);
							break;
						}
					}
				});
				AlertDialog alert = builder.create();
				alert.show();
				return false;
			}
		});

		this.reloadList();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.list_action, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			break;
		case R.id.mnu_create:
			Intent intent = new Intent(ShopListAct.this, EditShopAct.class);
			startActivityForResult(intent, EDIT_SHOP_ACT_EDIT);
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK && requestCode == EDIT_SHOP_ACT_EDIT) {
			Bundle resultBundle = data.getExtras();
			if (resultBundle.getBoolean("dataChanged")) {
				this.reloadList();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void reloadList() {
		List<String> shopsName = new ArrayList<String>();
		this.shopsId = new ArrayList<Integer>();
		for (Shops shop : ShopsDataSource.readAll()) {
			shopsName.add(shop.getName());
			this.shopsId.add(shop.getId());
		}

		ListView shoplist_list = (ListView) findViewById(R.id.shoplist_list);
		shoplist_list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, shopsName));
	}

	private void deleteShop(final int shopId) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getText(R.string.are_you_sure));
		builder.setMessage(getText(R.string.delete_shop));
		// yes
		builder.setPositiveButton(getText(R.string.yes), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				ShopsDataSource.delete(new Shops(shopId));
				ShopListAct.this.reloadList();
				dialog.dismiss();
			}
		});
		// no
		builder.setNegativeButton(getText(R.string.no), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
}
